#include <reg52.h>
#include "Key.h"
#include "Nixie.h"

sbit MOTOR = P1^0;       						//设定PWM输出的I/O端口,有个引脚输出
 
unsigned char count = 0;							//比较值
unsigned char Compare=0; 						    //pwm控制值

/*定时器T0初始化*/
void Com_Init()           
{
    TMOD |= 0x01;               		  //定时器T0设置成方式1
    TH0 = 0xff;                		    //定时常数 0.1ms 晶振为11.0592MHz
    TL0 = 0xa4;
    ET0 = 1; 													//中断标志    
    TR0 = 1; 													//定时器开关
    EA=1;   												  //总开关
}
	
/*T0中断初始化*/
void Time0_Init() interrupt 1 
{
	TR0 = 0; 												//定时器开关
	TH0 = 0xff;                  		//重装时间常数 0.1ms
	TL0 = 0xa4;
	count++;                       //比较值，很关键
	count %= 100;
	if(count <= Compare)        	//5==0° 10==90°
	{ 
		MOTOR = 1;          	  //高电平
	}
	else 
	{ 
		MOTOR = 0;             //低电平
	}
	TR0 = 1;                       //开启T0
}

unsigned int KeyNum,speed;

void main()
{
	Com_Init();							//中断初始化，打开定时器

	while(1)
	{	
		KeyNum = Key();
		if(KeyNum == 1)
		{
			speed++;
			speed %= 4;
			if(speed == 0){Compare = 10;}
			if(speed == 1){Compare = 25;}
			if(speed == 2){Compare = 75;}
			if(speed == 3){Compare = 100;}
		}
		Nixie_Scan(1,speed);
	}
}